import { resolve } from 'path'
import { defineConfig } from 'vite'
import { createVuePlugin as vue2 } from 'vite-plugin-vue2'
import ScriptSetup from 'unplugin-vue2-script-setup/vite'
import { baseBuildConfig, defaultPlugins } from '../vite.base.config'
import { minify } from 'uglify-es'

export const viteVue2Config = defineConfig({
  plugins: [vue2(), ScriptSetup({}), ...defaultPlugins],
  server: {
    port: 2000,
  },
  resolve: {
    alias: {
      'vue': resolve(__dirname, './node_modules/vue/dist/vue.runtime.esm.js'),
      'vue-demi': resolve(__dirname, '../node_modules/vue-demi/lib/v2/index.mjs'),
    },
  },
  build: {
    outDir: 'dist',
    rollupOptions: {
      plugins: [
        {
          name: 'uglifyjs',
          transform(code, id) {
            if (id.endsWith('.js')) {
              const result = minify(code, {
                compress: {
                  booleans_as_integers: true // 将布尔运算符转换为整数
                }
              })
              return result.code
            }
          }
        }
      ]
    }
  },
  ...baseBuildConfig,
})

export default viteVue2Config
